﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;

namespace zijian666.Http;

internal class TaskSyncSafe : ITaskSync
{
    public static ITaskSync Instance { get; } = new TaskSyncSafe();
    /// <summary>
    /// 同步执行异步方法
    /// </summary>
    public void RunSync(Func<Task> fn)
    {
        var task = fn();
        while (!task.IsCompleted)
        {
            Thread.Sleep(10);
        }
    }


    /// <summary>
    /// 同步执行异步方法
    /// </summary>
    public T RunSync<T>(Func<Task<T>> fn)
    {
        var task = fn();
        while (!task.IsCompleted)
        {
            Thread.Sleep(10);
        }
        return task.Result;
    }

}
